<?php
/*
 * @Author: your name
 * @Date: 2020-03-27 11:07:24
 * @LastEditTime: 2020-03-27 12:47:54
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \applet_api\application\api\controller\Common.php
 */
namespace app\api\controller;
use think\Controller;
use think\Request;
use think\Validate;
class Common extends Controller {
    protected $rules = array(
      'User' => array(
        'register' => array(
          'user_name' => 'require',
          'password'  => 'require|length:6',
        ),
        'login' => array(
          'user_name' => 'require',
          'password'  => 'require',
        ),
      ),
      'Code' => array(
        'get_code' => array(
            'username' => 'require',
            'is_exist' => 'require|number|length:1',
        ),
      ),
      'Article' => array(
        'get_articles' => array(
          'page' => 'require',
          'pageSize' => 'require'
        ),
        'create_article' => array(
          'title' => 'require',
          'category_id' => 'require'
        ),
        'get_article_detail' => array(
          'article_id' => 'require'
        )
      )
    );
    
    protected $request; // 用来处理参数
    protected $validater; // 用来验证数据/参数
    protected $params; // 过滤后符合要求的参数

    protected function _initialize() {
        parent::_initialize();
        $this->request = Request::instance();
        $this->params = $this->check_params($this->request->except(['time','token']));
    }
    /**
    * api 数据返回
    * @param  [int] $code [结果码 200:正常/4**数据问题/5**服务器问题]
    * @param  [string] $msg  [接口要返回的提示信息]
    * @param  [array]  $data [接口要返回的数据]
    * @return [string]       [最终的json数据]
    */
    public function return_msg($code, $msg = '', $data = []) {
      /*********** 组合数据  ***********/
      $return_data['code'] = $code;
      $return_data['msg']  = $msg;
      $return_data['data'] = $data;
      /*********** 返回信息并终止脚本  ***********/
      echo json_encode($return_data);die;
    }
    

    /**
    * 验证参数 参数过滤
    * @param  [array] $arr
    * @return [return]      [合格的参数数组]
    */
    public function check_params($arr) {
      /*********** 获取参数的验证规则  ***********/
      $rule = $this->rules[$this->request->controller()][$this->request->action()];
      /*********** 验证参数并返回错误  ***********/
      $this->validater = new Validate($rule);
      if (!$this->validater->check($arr)) {
          $this->return_msg(400, $this->validater->getError());
      }
      /*********** 如果正常,通过验证  ***********/
      return $arr;
    }
    
    /**
    * 校验用户名是否被占用
    * @param  [string]  
    * @return [return]      [合格的参数数组]
    */
    public function check_exist($value, $exist) {
      // $exist==0 就是注册，$exist==1就是登陆 
      $flag = $exist;
      
      $name_res = db('users')->where('user_name', $value)->find();
      switch ($flag) {
        case 0:
          if ($name_res) {
            $this->return_msg(400, '账号重复!');
        }
            break;
        case 1:
          if (!$name_res) {
            $this->return_msg(400, '账号不存在!');
        }
          break;
      }
    }

    /**
    * 校验设备是否归用户所有
    * @param  [string]  
    * @return [return]      [合格的参数数组]
    */
    public function check_user_devices($data) {
      $name_res = db('users')->where('id', $data['user_id'])->find();
      if(!$name_res) {
        $this->return_msg(400, '用户不存在!');
      }else{
        $user_devices = db('user_devices')->field('device_name')->where('user_name', $name_res['user_name'])->select();
        if(empty($user_devices)) {
          $this->return_msg(400, '该用户暂无设备!');
        }else{
          $isok = $this->deep_in_array($data['device_name'], $user_devices);
          if(!$isok) {
            $this->return_msg(400, '设备不存在!');
          }else{
            return;
          }
        }
      }
    }
    /**
    * 校验用户密码
    * @param  [string]  
    * @return [return]      [合格的参数数组]
    */
    public function check_user_password($data) {
      $user_res = db('users')->where('id', $data['user_id'])->find();
      if(!$user_res) {
        $this->return_msg(400, '用户不存在!');
      }else if($user_res['user_password'] != md5($data['password'])){
        $this->return_msg(400, '用户密码不正确!');
      }else{
        return;
      }

    }

    // 检查值是否在二维数组中
    public function deep_in_array($value, $array) {
      foreach($array as $item) {
        if(!is_array($item)) {
          if ($item == $value) {
            return true;
          } else {
            continue;
          }
        }
        if(in_array($value, $item)) {
          return true;
        } else if($this->deep_in_array($value, $item)) {
          return true;
        }
      }
      return false;
    }
  
  }